<?php
if(@$_POST['open'] != 'please'){
	exit;
}

include_once('../../php/include_all.php');
?>
<div class="row-fluid">
	<div class="box span12">
		<div class="widget-header widget-header-flat">
			<H5><i class="icon-calendar"></i>&nbsp; Master Program</H5>
		</div>
		
		<div class="box-content">
			<a href="#modalwin" data-toggle="modal" class="btn btn-small btn-primary2 no-radius btn-tambah"><i class="icon-plus"></i>&nbsp;Tambah Data</a>
			 &nbsp;<span id="load" class="spinner"></span>
		</div>
		
		<div id="modalwin" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		
		<header class="modal-header">
			<a href="#" class="close" data-dismiss="modal">x</a>
			<h3>Data program</h3>
		</header>
		
		<div class="modal-body">
		<form id="form_program" class="form-horizontal" name="masterProgram" method="post" action="">
		<input type="hidden" name="act" value="save_program">
		<input type="hidden" id="id_program" name="id_program" value="0">
			<div class="control-group">
				<label class="control-label">Nama Klasifikasi</label>
				<div class="controls">
				<select name="idKlasifikasi" id="id_klasifikasi">
					<option id="awal">Pilih salah satu</option>
					<?php
						$klas = get_datas('SELECT * FROM mus_klasifikasi');
						foreach($klas as $klas){
					?>
						<option value="<?php echo $klas['id_klasifikasi'] ?>"><?php echo $klas['nama_klasifikasi'] ?></option>
					<?php
					}
					?>
				</select>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">Kode Program</label>
				<div class="controls">
					
					<input type="text" name="kodeProgram" id="kodeProgram" readonly="">
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">Nama Program</label>
				<div class="controls">
					<input type="text" name="namaProgram" id="namaProgram" required>
				</div>
			</div>
			<div class="control-group">
				<div class="controls">
					<button class="btn btn-primary2 btn-small buttonSimpan" type="button"><span id="loader" class="pull-left" ></span><i class="icon-save"></i>&nbsp; Simpan</button>
					<span id="result"></span>
				</div>
			</div>
		</form>
		</div>
		</div>
		
		<div class="box-content">
		<table class="table table-hover table-bordered geo-table" width="100%">
			<thead>
			<tr>
				<th style="text-align:center; "width="5%;">No.</th>
				<th style="text-align:center; "width="15%;"> Klasifikasi</th>
				<th style="text-align:center; "width="15%;">Kode Program</th>
				<th style="text-align:center; "width="45%;">Nama Program</th>
				<th style="text-align:center; "width="20%;">Edit</th>
			</tr>
			</thead>
			<tbody id="tbody_program">
			<?php
				$no = 1;
				$data = get_datas("SELECT p.id_program, p.kode_program, p.nama_program, k.nama_klasifikasi FROM mus_program p, mus_klasifikasi k WHERE p.id_kla = k.id_klasifikasi order by id_kla, kolom");
				foreach ($data as $data){
			?>
			<tr>
				<td style="text-align:center;"><?php echo $no?></td>
				<td><?php echo $data['nama_klasifikasi']?></td>
				<td><?php echo $data['kode_program']?></td>
				<td><?php echo $data['nama_program']?></td>
				<td style="text-align:center;">
					<a href="#modalwin" data-toggle="modal" class="btn btn-info btn-small bt-edit-program" name="<?php echo $data['id_program']; ?>"><i class="icon-edit icon-white"></i>&nbsp; Ubah</a>
					<a class="btn btn-danger btn-small bt-delete-program" name="<?php echo $data['id_program']; ?>"><i class="icon-trash icon-white"></i>&nbsp; Hapus</a></td>
			</tr>
			<?php
				$no++;
			}
			?>
			<tbody>
		</table>
		</div>
	</div>
</div>
<script>
var url = 'admin/master_program/program_save.php';

$('#id_klasifikasi').change(function(){
	source ="admin/master_program/name.php?ids="+this.value;
	var hasil=$.get(source);
	hasil.done(function(data){
		document.getElementById('kodeProgram').value=data;	
	});
});

var result = $('#result');

	$('.buttonSimpan').click(function(){
		var btn = $(this);
		var load = $('#loader');
		
		 load.addClass('icon-spinner icon-spin icon-2x white');
		
	//	btn.removeClass('btn-primary').addClass('btn-success');
		
		var form = $('#form_program');
		var data = form.serializeArray();
		
		var post = $.post(url, data);
		post.done(function(res){
			var hasil = res.split('__');
			if(hasil.length == 3){
				if(hasil[0] == 'success'){
					//$('#id_program').val(result[1]);
					result.html('<font color="green">Data Tersimpan...</font>');
					setTimeout(function(){
						result.html('');
					}, 1500);
					
					var tbody = $('#tbody_program');
					tbody.html(hasil[2]);
					ready();
					$("#id_program").val("0");
					$("#kodeProgram").val("");
					$("#namaProgram").val("");
					document.getElementById("awal").selected = true;
					
				 	$('#modalwin').modal('hide');
				}else{
					result.html('<font color="red">'+res+'</font>');
				}
			}else{
				result.html('<font color="red">'+res+'</font>');
			}
			load.removeClass();
			btn.removeClass('btn-success').addClass('btn-primary');
		});
	});
	
	$('.btn-tambah').click(function(){
		$("#id_program").val("0");
		$("#kodeProgram").val("");
		$("#namaProgram").val("");
		document.getElementById("awal").selected = true;
	});
function ready(){

	$('.bt-delete-program').click(function(){
		var btn = $(this);
		var pid  = this.name;
		var load = $(this);
		load.html('<div class="icon-spinner icon-spin icon-2x white"></div>');
        bootbox.confirm("Anda akan menghapus program ini?", function(hasil) {
			if(hasil==true){
				if(pid!=0){
					var post = $.post(url,{act:"del_program",idPro:pid});
					post.done(function(res){
					var hasil = res.split('__');
						if(hasil.length == 3){
							if(hasil[0] == 'success'){
								result.html('<font color="green">Data Telah Dihapus...</font>');
								setTimeout(function(){
									result.html('');
								}, 1500);
								var tbody = $('#tbody_program');
								tbody.html(hasil[2]);
								ready();
							//btn.parent().parent().remove();
							}
						}
					});
					load.html('<i class="icon-trash icon-white"></i>&nbsp; Delete');
				}
			}else{
				load.html('<i class="icon-trash icon-white"></i>&nbsp; Delete');
			}
		});
	});
	
	$('.bt-edit-program').click(function(){
		var idPro= this.name;
		var form = $('#form_program');
		var post = $.post(url,{act:'edit_program',idPro:idPro});
		post.done(function(res){
		console.log(res);
			var value = res.split('__');
			$('#id_program').val(value[0]);
			form.find('select[name="idKlasifikasi"]').val(value[1]);
			form.find('input[name="kodeProgram"]').val(value[2]);
			form.find('input[name="namaProgram"]').val(value[3]);
		});
		
	});
}
 /*	
$('.close').click(function(){
//	$('#id_program').val(0);
	document.getElementById('form_program').reset();
});	 */

ready();
</script>